package com.link510.aitools.framework.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.common.base.Strings;
import com.link510.aitools.R;
import com.link510.aitools.core.domain.qiniu.QiniuFileInfo;

import java.util.List;

public abstract class PictureAdapter extends BaseAdapter {

    private static final String TAG = "PictureAdapter";

    private LayoutInflater mInflator;

    private Context mContext = null;

    private String baseImageUrl = null;


    public PictureAdapter(Context mContext) {
        this.mContext = mContext;
        mInflator = LayoutInflater.from(mContext);
        baseImageUrl = Utils.getApp().getString(R.string.base_cdn_url);
    }

    private PictureAdapter() {

    }

    /**
     * 调用列表
     *
     * @return List<ActionInfo>
     */
    public abstract List<QiniuFileInfo> getPictureList();

    /**
     * 处理方法
     *
     * @param info 动作
     */
    public abstract void toAction(QiniuFileInfo info);

    @Override
    public int getCount() {
        return getPictureList().size();
    }

    @Override
    public Object getItem(int position) {
        return getPictureList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final QiniuFileInfo info = getPictureList().get(i);

        if (info == null || Strings.isNullOrEmpty(info.getKey())) {
            return view;
        }

        ViewHolder viewHolder = null;

        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.item_picture, null);
            viewHolder = new ViewHolder();
            viewHolder.litpicPicture = (ImageView) view.findViewById(R.id.litpic_imageview);
            viewHolder.titleTextView = (TextView) view.findViewById(R.id.title_textview);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }


        String[] keys = info.getKey().split("/");

        if (keys.length >= 1) {
            viewHolder.titleTextView.setText(keys[keys.length - 1]);
        }

        String litpic = baseImageUrl + info.getKey();

        Glide.with(mContext).load(litpic)
                .override(80, 80)
                .thumbnail(Glide.with(mContext).load(R.mipmap.loading))
                .fitCenter()
                .into(viewHolder.litpicPicture);


        view.setOnClickListener(view1 -> {
            toAction(info);
            Log.i(TAG, "onClick: " + info);
        });

        return view;
    }

    static class ViewHolder {

        ImageView litpicPicture;
        TextView titleTextView;
    }
}
